package com.btex.app.tools;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.btex.app.OTCAddBankActivity;
import com.btex.app.OTCAddWeiAndZhi;
import com.btex.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangyi<zhangyi, 1104745049@qq.com>
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/8/13
 * @time 下午4:48
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


public class OTCPaymentMethodsDialog extends Dialog {
    private final Context context;

    private final SharedPreferencesUtil mSp;
    @BindView(R.id.dialog_otc_payment_bank_imag)
    ImageView mDialogOtcPaymentBankImag;
    @BindView(R.id.dialog_otc_payment_bank)
    LinearLayout mDialogOtcPaymentBank;
    @BindView(R.id.dialog_otc_payment_zhi_imag)
    ImageView mDialogOtcPaymentZhiImag;
    @BindView(R.id.dialog_otc_payment_zhi)
    LinearLayout mDialogOtcPaymentZhi;
    @BindView(R.id.dialog_otc_payment_wei_imag)
    ImageView mDialogOtcPaymentWeiImag;
    @BindView(R.id.dialog_otc_payment_wei)
    LinearLayout mDialogOtcPaymentWei;


    public OTCPaymentMethodsDialog(Context context) {
        super(context, R.style.coupon_style);
        this.context = context;

        mSp = SharedPreferencesUtil.getSharedPreferences(context);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otc_payment_methods_dialog);
        ButterKnife.bind(this);

        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.9);
//        attributes.height = (int) (heightPixels * 0.6);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }


//    private void SnackbarShow(String ms) {
//
//        Snackbar snackbar = ShortSnackbar(mDialogOtcPaymentWeiImag, ms, Color.parseColor("#ffffff"), Color.parseColor("#272736"));
//        snackbar.show();
//    }
//
//    public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
//        Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
//        setSnackbarColor(snackbar, messageColor, backgroundColor);
//        return snackbar;
//    }
//
//    /**
//     * 设置Snackbar文字和背景颜色
//     *
//     * @param snackbar
//     * @param messageColor
//     * @param backgroundColor
//     */
//    public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//        View view = snackbar.getView();
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            TextView message = (TextView) snackbar.getView().findViewById(R.id.snackbar_text);
//            //View.setTextAlignment需要SDK>=17
//            message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
//            message.setGravity(Gravity.CENTER);
//        }
//
//        if (view != null) {
//            view.setBackgroundColor(backgroundColor);
//            ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
//        }
//    }


    @OnClick({R.id.dialog_otc_payment_bank, R.id.dialog_otc_payment_zhi, R.id.dialog_otc_payment_wei, R.id.dialog_otc_payment_tianya})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_otc_payment_bank:
                setImag();
                mDialogOtcPaymentBankImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_xuanzhe));
                context.startActivity(new Intent(context, OTCAddBankActivity.class));
                dismiss();
                break;
            case R.id.dialog_otc_payment_zhi:
                setImag();
                mDialogOtcPaymentZhiImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_xuanzhe));
                context.startActivity(new Intent(context, OTCAddWeiAndZhi.class).putExtra("data", "支付宝"));
                dismiss();
                break;
            case R.id.dialog_otc_payment_wei:
                setImag();
                mDialogOtcPaymentWeiImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_xuanzhe));
                context.startActivity(new Intent(context, OTCAddWeiAndZhi.class).putExtra("data", "微信"));
                dismiss();
                break;

            case R.id.dialog_otc_payment_tianya:
                setImag();
                mDialogOtcPaymentWeiImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_xuanzhe));
                context.startActivity(new Intent(context, OTCAddWeiAndZhi.class).putExtra("data", "天涯"));
                dismiss();
                break;
        }
    }

    private void setImag() {
        mDialogOtcPaymentBankImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_nomal_xuan));
        mDialogOtcPaymentZhiImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_nomal_xuan));
        mDialogOtcPaymentWeiImag.setBackground(context.getResources().getDrawable(R.mipmap.otc_nomal_xuan));

    }
}
